package com.wang.common.util;

import com.wang.common.pojo.BasePojo;

/**
 * 分页工具类
 * 
 * @author Zjx
 *
 */
public class PageInfoUtil extends BasePojo<PageInfoUtil>
{
	/**
	 * 每页条数
	 */
	private int pageSize = 20;
	/**
	 * 当前页
	 */
	private int currentPage;
	/**
	 * 总条数
	 */
	private int totalRecord;
	/**
	 * 总页数
	 */
	private int totalPage;
	/**
	 * 上一页
	 */
	private int prePage;
	/**
	 * 下一页
	 */
	private int nextPage;
	/**
	 * 当前页的起始条数 
	 * 共45条记录，
	 * 每页10条 页 
	 * 当前页的起始条数 
	 * 1 1 
	 * 2 11 
	 * 3 21 
	 * 4 31
	 */
	private int currentRecord;

	public int getPageSize()
	{
		return pageSize;
	}

	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}

	public int getCurrentPage()
	{
		if(this.currentPage > this.getTotalPage() && this.getTotalRecord() > 0)
		{
			this.currentPage = this.getTotalPage() ; 
		}
		if(this.currentPage < 1)
		{
			this.currentPage = 1 ;
		}
		return currentPage;
	}

	public void setCurrentPage(int currentPage)
	{
		this.currentPage = currentPage;
	}

	public int getTotalRecord()
	{
		return totalRecord;
	}

	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}

	public int getTotalPage()
	{
		/**
		 * 每页10条,共50条;5页
		 * 每页10条,共55条;6页
		 */
		this.totalPage = this.getTotalRecord() / this.getPageSize() ; 
		if(this.getTotalRecord() % this.getPageSize() != 0)
		{
			this.totalPage = this.totalPage + 1 ; 
		}
		return totalPage;
	}

	public int getPrePage()
	{
		this.prePage = this.getCurrentPage() - 1 ; 
		if(this.prePage < 1 )
		{
			this.prePage = 1 ; 
		}
		return prePage;
	}

	public int getNextPage()
	{
		this.nextPage = this.getCurrentPage() + 1 ;
		if(this.nextPage > this.getTotalPage())
		{
			this.nextPage = this.getTotalPage() ; 
		}
		return nextPage;
	}

	/**
	 * 当前页的起始条数 
	 * 共45条记录，
	 * 每页10条 页 
	 * 当前页的起始条数 
	 * 1 1 
	 * 2 11     (2 - 1) * 10  + 1 
	 * 3 21 	(3 - 1) * 10  + 1 
	 * 4 31		(4 - 1) * 10  + 1
	 */
	public int getCurrentRecord()
	{
		this.currentRecord = (this.getCurrentPage() - 1) * this.getPageSize() ; 
		return currentRecord;
	}
	
	public static void main(String[] args)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		pageInfoUtil.setTotalRecord(45);
		pageInfoUtil.setCurrentPage(1);
		pageInfoUtil.setPageSize(10);
		
		System.out.println(pageInfoUtil.toJSON());
	}
}
